package com.chen.recruit.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.recruit.common.annotation.Log;
import com.chen.recruit.common.api.CommonResult;
import com.chen.recruit.common.enums.BusinessType;
import com.chen.recruit.common.utils.ExcelUtils;
import com.chen.recruit.common.valid.AddGroup;
import com.chen.recruit.common.valid.UpdateGroup;
import com.chen.recruit.domain.Post;
import com.chen.recruit.domain.model.PageQuery;
import com.chen.recruit.service.PostService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "岗位管理 PostController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/post")
public class PostController {

    private final PostService postService;

    @ApiOperation("新增岗位")
    @SaCheckPermission("system:post:add")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated(AddGroup.class) @RequestBody Post post) {
        return postService.addOrUpdatePost(post);
    }

    @ApiOperation("删除岗位")
    @SaCheckPermission("system:post:delete")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return postService.deletePostByIds(ids);
    }

    @ApiOperation("更新岗位")
    @SaCheckPermission("system:post:update")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult update(@Validated(UpdateGroup.class) @RequestBody Post post) {
        return postService.addOrUpdatePost(post);
    }

    @ApiOperation("获取岗位详细信息")
    @SaCheckPermission("system:post:update")
    @GetMapping("/{id}")
    public CommonResult getInfo(@PathVariable Integer id) {
        return CommonResult.success(postService.getById(id));
    }

    @ApiOperation("查询岗位列表")
    @SaCheckPermission("system:post:list")
    @GetMapping("/list")
    public CommonResult list(Post post, PageQuery pageQuery) {
        return CommonResult.success(postService.page(pageQuery.build(), buildQueryWrapper(post)));
    }

    @ApiOperation("导出岗位列表")
    @SaCheckPermission("system:post:export")
    @Log(module = "系统管理-岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Post post) {
        ExcelUtils.exportExcel(response, "岗位表", Post.class, postService.list(buildQueryWrapper(post)));
    }

    private LambdaQueryWrapper<Post> buildQueryWrapper(Post post) {
        return Wrappers.<Post>lambdaQuery()
                .like(StringUtils.isNotBlank(post.getName()), Post::getName, post.getName())
                .eq(ObjectUtil.isNotNull(post.getStatus()), Post::getStatus, post.getStatus());
    }

}
